﻿using DevTeam;
using System;
using System.Data;

namespace ConfExpert.Utility
{
    public class Mail
    {
        public class MailTemplate
        {
            public string To;
            public string Subject;
            public string Body;
            public string CC;
        }

        public static MailTemplate GetTemplate(int TypeId, string Param1)
        {
            if (Session.DataManager.ConnectionString.ToLower().Contains("test"))
                TypeId = 100;

            DataTable dt = Session.DataManager.ExecuteDataTable("GetMailTemplate", TypeId, Param1);
            MailTemplate template = new MailTemplate();
            DataRow dr = dt.Rows[0];
            template.To = dr["To"].ToString();
            template.Subject = dr["Subject"].ToString();
            template.Body = dr["Body"].ToString();
            template.CC = dr["CC"].ToString();
            return template;
        }

        private static ceMessenger.ceMessengerSoapClient messenger;

        public static void Init()
        {
            messenger = new ceMessenger.ceMessengerSoapClient();
            messenger.Endpoint.Address = new System.ServiceModel.EndpointAddress(string.Format("http://{0}/ceMessenger/ceMessenger.asmx", Session.ServerAddress));
        }


        public static void Send(string to, string subject, string body, string commaSeperatedCCMails, byte[] File, string FileName, string ReplyTo)
        {
            if (Session.isProduction)
                messenger.SendEmailWithAttachmentFileNamex(Session.MessengerUserName, Session.MessengerPassword, "Karbel Software Team", to, commaSeperatedCCMails, subject, body, File, FileName, ReplyTo);
        }

        public static void Send(string to, string subject, string body, string commaSeperatedCCMails, string ReplyTo = "", string command = "")
        {
            try
            {
                if (Session.isProduction)
                    messenger.SendEmail(Session.MessengerUserName, Session.MessengerPassword, "Karbel Software Team", to, subject, body, commaSeperatedCCMails);

            }
            catch (Exception ex)
            {
                LogManager.LogToDB(ex);
            }
        }
    }
}